<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use Imi\Util\Random;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;
use Imi\Db\Db;

/**
 * Model操作 演示
 * @Controller("/Model/")
 * @View(renderType="json")
 */
class ModelController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/test
     * 
     * @Action
     * @return void
     */
    public function test()
    {
        $list = Test::select([
            'b'    =>    'bbb',
        ]);
        return [
            'list'    =>    $list,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/find
     * 
     * @Action
     * @return void
     */
    public function find()
    {
        $model = User::find(1);

        return $model;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/select
     * 
     * @Action
     * @return void
     */
    public function select()
    {
        $list = User::select();

        return $list;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/newInstance
     * 
     * @Action
     * @return void
     */
    public function newInstance()
    {
        $user = User::newInstance();

        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/insert
     * 
     * @Action
     * @return void
     */
    public function insert()
    {
        $user = User::newInstance();
        $user->username = Random::letterAndNumber(6, 16);
        $user->ex->intro = '这个人很懒，什么也没留下';
        $result = $user->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $user = User::find(1);
        $user->ex->intro = '这个人很懒，什么也没留下-' . date('Y-m-d H:i:s');
        $result = $user->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = User::query()->max('id');
        $user = User::find($maxId);
        $result = $user->delete();

        return [
            'id'            =>    $maxId,
            'affectedRows'    =>    $result->getAffectedRows(),
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/save
     * 
     * @Action
     * @return void
     */
    public function save()
    {
        $user = User::find(1);
        $user->ex->intro = '这个人很懒，什么也没留下-' . date('Y-m-d H:i:s');
        $result = $user->save();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/Model/test2
     * 
     * @Action
     * @return void
     */
    public function test2()
    {
        $result = Db::query()->table('tb_user')->setData([
            'username'    =>    '111',
        ])->insert();
        var_dump($result->getAffectedRows());

        $result = Db::query()->table('tb_user')->insert([
            'username'    =>    '222',
        ]);
        var_dump($result->getAffectedRows());

        $result = Db::query()->table('tb_user')->where('id', '=', 1)->setFieldInc('age')->setField('username', 'ttt')->update();
        var_dump($result->getAffectedRows());

        $result = Db::query()->table('tb_user')->where('id', '=', 2)->setFieldDec('age')->setFieldExp('username', '\'t2\'')->update();
        var_dump($result->getAffectedRows());


    }
}